import { _decorator, Component, Node } from 'cc';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { v3 } from 'cc';
import { Vec3 } from 'cc';
import GlobalData from '../../Init/Config/GlobalData';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { Camera } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GuideCmp')
export class GuideCmp extends Component {
    @property(Node)
    protected guideFinger: Node = null;

    curfingerPos = v3();
    tarFingerPos = v3();

    protected onLoad(): void {
        EventManager.on(EventTypes.UIEvents.ShowCircleMask, this.onShowCircleMask, this);
    }
    onEnable() {

    }

    update(dt: number) {
        this.curfingerPos.lerp(this.tarFingerPos, 0.2);
        this.guideFinger.setPosition(this.curfingerPos);
    }

    onShowCircleMask(d: { pos: Vec3, clickCb: Function, useAnim?: boolean, is3D?: boolean }) {

        const pos2d = v3(d.pos);
        // 3d坐标 -> 屏幕坐标
        if (d.is3D) {
            const camera = GlobalData.get(GlobalEnum.GlobalDataType.Camera3D) as Camera;
            camera.convertToUINode(d.pos, this.node, pos2d);
        }
        this.tarFingerPos.set(pos2d);
        if (!d.useAnim) {
            this.curfingerPos.set(pos2d);
            this.guideFinger.setPosition(this.curfingerPos);
        }
    }
}


